T/SQL stands for Transact Extension of Structure Query Language.
Differences Between SQL and T/SQL
T/SQL is a procedural language whereas SQL is a non-procedural language.
SQL supports the single line stint execution process only, whereas T/SQL supports the multiple stints execution process.
SQL doesn’t support conditional and control statements like if, if-else, multiple if else, nested if-else and while loop control statement. But these are possible to implement under T/SQL.
We can’t implement Try and Catch handling methods in SQL, whereas we can implement these in T/SQL.
Declaration of variables are not supported in SQL but they are supported In T/SQL.
SQL doesn’t provide re-usability and security facilities to the query information, whereas T/SQL provides re-usability and security facilities by using the database objects which are stored procedures, stored function, and database triggers.
In T/SQL the user will write the stints under a Block.
In this case, every SQL stint is executed individually by the server so that the burden of the server will be increased and application performance will be reduced.
Query Execution Process in T/SQL
In this case, the SQL stint is executed as a single unit by the server so that the burden of the server will be reduced and application performance will be increased.
Types of Blocks
In T/SQL the user will interact with two types of blocks,
Anonymous Block | Sub Block |
It is unnamed block | It is named block |
These are temporary blocks | These are permanent blocks |
It will not be saved into database | These are saved into the database |
Every time compilation and execution of the program | These are pre-compiled program |
These consume more time | These are time-saving blocks |
It will not provide re-usability and security | It will provide re-usability and security |
Working With Anonymous Blocks
When we work with Anonymous Blocks we should follow the following steps.
Step 1
Declare Variable: When we declare variables under T/SQL Blocks we should use Declare keyword and prefix symbol (@)
Syntax
Declare @<variable Name> <Datatype>[Size]
Example
Declare @a int,@name varchar(50)
Step 2
Assigning the values to the variables: When we assign the values to the variables we should use the Set keyword as below:
Syntax
set @<variable name >=value
Example
set @a=10;
Set @name='Sam'
Step 3
To display the variable values or message on the screen: When we display the variable values and message then we use the print statement.
Syntax
Print <variable name>
Example
Print @a;
Print @name
Example
Write a program to perform arithmetic operation on given values,
- Declare @a int,@b int,@c int
- Set @a=12;set @b=4;
- Set @c=@a+@b;
- Print 'Add is:'+cast(@c as char)
- Set @c=@a-@b;
- Print 'Sub is:'+cast(@c as char)
- Set @c=@a*@b;
- Print 'Multiple is:'+cast(@c as char)
- Set @c=@a/@b;
- Print 'Div is:'+cast(@c as char)
Result
Summary
In this article, we have learned about T/SQL and the difference between SQL and T/SQL. We also learned about the block and query execution process. In the next article of this series, we will learn about cursor, store procedure, store function, and triggers.